package com.example.device.device

import android.content.Intent
import android.text.Editable
import androidx.activity.viewModels

import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ToastUtils
import com.example.common.view.view.BaseMvvmActivity
import com.example.device.R
import com.example.device.databinding.ActivityDeviceOrderAddBinding
import com.example.device.entity.DeviceOrderAddParam
import com.example.device.vm.DeviceOrderViewModel
import com.example.image.glide.GlideImgLoader
import com.github.gzuliyujiang.wheelpicker.DatePicker
import com.github.gzuliyujiang.wheelpicker.OptionPicker
import com.github.gzuliyujiang.wheelpicker.entity.DateEntity
import com.wildma.pictureselector.PictureBean
import com.wildma.pictureselector.PictureSelector
import dagger.hilt.android.AndroidEntryPoint
import java.text.SimpleDateFormat

@AndroidEntryPoint
@Route (path = "/device/DeviceOrderAddActivity")
class DeviceOrderAddActivity : BaseMvvmActivity<ActivityDeviceOrderAddBinding>() {
    override fun getLayoutId(): Int = R.layout.activity_device_order_add
    private val deviceOrderViewModel:DeviceOrderViewModel by viewModels()
    override fun initView() {
        super.initView()
        mDataBinding.ivBack.setOnClickListener {
            finish()
        }
        //类型选择器
        mDataBinding.ivType.setOnClickListener {
            val list = mutableListOf("设备巡检","安防巡检","其它")
            val optionPiker= OptionPicker(this)
            optionPiker.setData(list)
            optionPiker.setTitle("选择工单类型")
            optionPiker.setOnOptionPickedListener { position, item ->
                mDataBinding.edType.text= Editable.Factory.getInstance().newEditable(list[position])
            }
            optionPiker.show()
        }
        //时间选择器
        mDataBinding.ivTime.setOnClickListener {
            val dataPicker= DatePicker(this)
            dataPicker.setTitle("请选择时间")
            dataPicker.wheelLayout.setRange(
                DateEntity.target(1900,1,1), DateEntity.target(2600,12,31),
                DateEntity.today())
            dataPicker.setOnDatePickedListener { year, month, day ->
                mDataBinding.edTime.text= Editable.Factory.getInstance().newEditable("$year-$month-$day")
            }
            dataPicker.show()
        }
        //添加图片
        mDataBinding.ivIcon.setOnClickListener {
            PictureSelector.create(this,200).selectPicture()
        }
        mDataBinding.btnCommit.setOnClickListener {
            val deviceOrderAddParam=DeviceOrderAddParam(
             address="八维南门",
             appointmentTime=SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(System.currentTimeMillis()),
             content=mDataBinding.edContent.text.toString(),
             deviceInspections= emptyList(),
             id=0,
             inspector="牛马玻璃姐",
             orderType=mDataBinding.edType.text.toString(),
             photos= emptyList(),
             status="新建",
            )
            deviceOrderViewModel.getListDataDeviceOrderAdd(deviceOrderAddParam)
        }
        deviceOrderViewModel.mSuccessLiveData.observe(this){
            ToastUtils.showLong("添加成功")
            finish()
        }
        deviceOrderViewModel.mErrorLiveData.observe(this){
            ToastUtils.showLong(it)
        }
    }
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode==200&&resultCode== RESULT_OK){
            val bean=data?.extras?.get(PictureSelector.PICTURE_RESULT) as PictureBean
            GlideImgLoader.loadImage(this,bean.path,mDataBinding.ivIcon)
        }
    }
}